from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from appium.webdriver.common.mobileby import MobileBy

from pykeyboard import PyKeyboard  # 在PyUserInput中，需要额外引入pypiwin32
import re
import time
import random
import win32api
import win32con


# 快照
def qshot(driver):
    driver.save_screenshot('../Report/log/' + str(time.time()) + ".png")


# 模拟键盘按下 对应关系[character:按键 / n:按键次数 / interval:按键间隔]
def kpress(character, n=1, interval=0):
    PyKeyboard().tap_key(character=character, n=n, interval=interval)


# 强制等待[num:秒]
def wait(num):
    time.sleep(num)


# 显式等待[driver:浏览器对象 / element:路径 / kind:方式 / device:设备类型]
def if_wait(driver, element, kind=0, device=0):
    # 设备类型区分
    if device == 'Mobile' or device == 1:
        by = MobileBy
    elif device == 'PC' or device == 0:
        by = By

    # 路径类型区分
    if kind == 'xpath' or kind == 0:
        target = EC.presence_of_element_located((by.XPATH, element))
    elif kind == 'id' or kind == 1:
        target = EC.presence_of_element_located((by.ID, element))
    elif kind == 'css' or kind == 2:
        target = EC.presence_of_element_located((by.CSS_SELECTOR, element))
    elif kind == 'ai' or kind == 3:
        target = EC.presence_of_element_located((by.ACCESSIBILITY_ID, element))

    # 执行显式等待
    el = WebDriverWait(driver, 30, 0.5).until(target)
    return el


# 模拟鼠标滚轮滑动 -1向下 1向上
def gulu(flag=-1, step=3000):
    for j in range(0, step):
        win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, flag)


# 获取屏幕像素大小
def size(driver, flag=''):
    size = driver.get_window_size()
    if (flag == 'x'):
        return size['width']
    elif (flag == 'y'):
        return size['height']
    else:
        return size


# 身份证号生成
def id_number_create(number=None):
    city_num = ['620123', '340311', '231100', '330122', '230406', '513322', '653129', '451324', '370522', '650102',
                '512002', '630106', '331081', '320831', '654326', '310113', '513337', '632224', '410611', '321203',
                '320500', '410302', '231000', '211303', '530624', '130306', '330302', '130406', '370681', '530423',
                '330922', '430511', '141023', '530300', '621200', '430203', '141082', '231083', '530481', '530600',
                '513228', '150400', '411481', '411725', '150823', '371502', '450681', '350702', '431302', '320311']
    year_num = ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012',
                '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022']
    mouth_num = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
    day_num = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17',
               '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28']
    w = ['7', '9', '10', '5', '8', '4', "2", '1', '6', '3', '7', '9', '10', '5', '8', '4', '2']

    if number is None:
        id_name = random.choice(city_num) + random.choice(year_num) + random.choice(mouth_num) + random.choice(day_num)

        for j in range(3):
            id_name += str(random.randint(0, 9))

        su = 0
        for k in range(len(id_name)):
            su += int(id_name[k]) * int(w[k])

        endl = su % 11
        if endl == 10:
            endl = 'x'
        else:
            endl = str(endl)

        return id_name + endl

    else:
        city = number[:6]
        year = number[6:10]
        mouth = number[10:12]
        day = number[12:14]

        print(city)
        if year in year_num and mouth in mouth_num and day in day_num:
            su = 0
            for i in range(len(number) - 1):
                su += int(number[i]) * int(w[i])

            endl = su % 11
            if endl == int(number[-1]):
                return True
        return False


# luhn校验 //可用于银行卡
def luhn(card_num):
    s = 0
    card_num_length = len(card_num)
    for _ in range(1, card_num_length + 1):
        t = int(card_num[card_num_length - _])
        if _ % 2 == 0:
            t *= 2
            s += t if t < 10 else t % 10 + t // 10
        else:
            s += t
    return s % 10 == 0


# 获取合法的银行卡号
def get_cardnum(start_with='622609', total_num=16):
    result = start_with

    # 随机生成前N-1位
    while len(result) < total_num - 1:
        result += str(random.randint(0, 9))

    # 计算前N-1位的校验和
    s = 0
    card_num_length = len(result)
    for _ in range(2, card_num_length + 2):
        t = int(result[card_num_length - _ + 1])
        if _ % 2 == 0:
            t *= 2
            s += t if t < 10 else t % 10 + t // 10
        else:
            s += t

    # 最后一位当做是校验位，用来补齐到能够整除10
    t = 10 - s % 10
    result += str(0 if t == 10 else t)
    return result


# 获取电话号码
def phone_number():
    while True:
        num = str(random.randint(10000000000, 20000000000))
        if phone_check(num):
            return num


def phone_check(number):
    pattern = re.compile(r'^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$')
    return (pattern.search(number) is not None)


def email_number():
    font = random_char(random.randint(5, 15))
    mid = random_char(random.randint(2, 8))
    end = random_char(random.randint(3, 5))

    return font + '@' + mid + '.' + end


def email_check(number):
    pattern = re.compile(r'^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$')
    return (pattern.search(number) is not None)


class RandSTR():

    # 组合
    def random_compose(self):
        rand = {
            'random_char': 20,
            'random_legal': 80
        }
        pos = random.randint(0,10000)




    def random_char(num=1):
        rand = [52, 80, 110]
        s = ''
        for i in range(num):
            k = random.randint(33, 126)
            k = k - pow(k - random.choice(rand), 0.5)
            k = int(abs(k))
            s += chr(k)
        print()
        return s


    def random_legal(num=1):
        rand = [(48, 57), (97, 122), (65, 90)]
        s = ''
        for i in range(num):
            r = random.choice(rand)
            k = random.randint(r[0], r[1])
            s += chr(k)
        return s

# 警告格式
# def warring(self, s, end='\n'):
#     print(f'\033[0;1;31m{s}\033[0m', end=end)
